< previous page page_207 next page >

Page 207
you to specify another condition to evaluate whether the first condition is False. Using the ElseIf statement, you can evaluate any number of conditions with one If statement block. The following lines of code demonstrate how you can use ElseIf to test for three possibilitieswhether the contents of the variable fTest are negative, zero, or positive:
    If fTest < 0 Then
        lblResult.Caption = "Negative"
    ElseIf fTest = 0 Then
        lblResult.Caption = "Zero"
    Else
        lblResult.Caption = "Positive"
    End If
The preceding code works by first evaluating the condition in the If statement. If the condition is True, the statement (or statements) immediately following the If statement is executed; then the program skips to the first statement after the End If statement.
If the first condition is False, the program skips to the first ElseIf statement and evaluates its condition. If this condition is True, the statements following the ElseIf are executed, and control again passes to the statement after the End If. This process continues for as many ElseIf statements as are in the block.
If all the conditions are False, the program skips to the Else statement and processes the commands between the Else and the End If statements. The Else statement is not required.
Using Select Case
Another way to handle decisions in a program is to use the Select Case statement. It allows you to conditionally execute any of a series of statement groups based on the value of a test expression, which can be a single variable or a complex expression. The Select Case statement is divided into two parts: a test expression to be evaluated and a series of Case statements listing the possible values.
How Select Case Works
The Select Case structure is similar to a series of If/Then/ElseIf statements. The following lines of code show the syntax of the Select Case block:
Select Case testvalue
   Case value1
      statement group 1
   Case value2
      statement group 2
End Select
The first statement of the Select Case block is the Select Case statement itself. This statement identifies the value to be tested against possible results. This value, represented by the testvalue argument, can be any valid numeric or string expression, including literals, variables, or functions.
Each conditional group of commands (those that are run if the condition is met) is started by a Case statement. The Case statement identifies the expression to which the testvalue is compared. If the testvalue is equal to the expression, the commands after the Case statement are

 
< previous page page_207 next page >